<?php
//démarrage des sessions
@session_start();

set_include_path('../../offline');
require_once('functions.php');

//fonction pour charger automatiquement les classes
spl_autoload_register('generic_autoload');

//définition de la connexion à la base
$pdo = new myPdo('mysql:host=localhost;dbname=associations', 'root', '7au864pq');
Controller_Template::$db = $pdo;

//configuration de l'appli
Config::set_slot('root', 'http://associations-genlis.fr/admin/');
Config::set_slot('home_max_actualites_image', 3);
Config::set_slot('home_max_actualites_simple', 15);
Config::set_slot('actualites_max_page', 10);
Config::set_slot('categories_max_page', 10);
Config::set_slot('associations_max_page', 10);
Config::set_slot('communes_max_page', 10);

//ajoute admin.css à toutes les pages d'administration
add_css(Config::get_slot('root', '') . '../css/admin.css');

//analyse l'uri
preg_match(
    '#^/admin/([a-z]*)[/]{0,1}([a-z0-9_-]*)[\.html]*[?]{0,1}(.*)$#',
    $_SERVER['REQUEST_URI'],
    $match);
//var_dump($match);

//temporisation de sortie
ob_start();

//procédure de login
if(isset($_SESSION['idassoc']) && is_numeric($_SESSION['idassoc']))
//if(true)
{
    if(!empty($match[1]))//module
    {
        //récupère l'action
        if(!empty($match[2]))
        {
            $action = $match[2];
        }
        else//sinon action index
        {
            $action = 'index';
        }

        try
        {
            switch($match[1])
            {
                case 'login' :
                {
                    switch($action)
                    {
                        case 'logout':
                            $controller = Controller_Login::getInstance();
                            $controller->logout();
                            break;
                        default:
                            throw new Exception('Page non trouvée');
                            break;
                    }
                    break;
                }
                case 'categories':
                    Config::set_slot('active', 'categories');

                    switch($action)
                    {
                        case 'index':
                            $controller = Controller_Categorie::getInstance();
                            $controller->index();
                            break;
                        case 'edit':
                            if(!empty($match[3])) //argument
                            {
                                $args = explode('&', $match[3]);
                                $args = explode('=', $args[0]);
                                $args[0] = trim($args[0]);
                                $args[1] = trim($args[1]);
                                if($args[0] == 'id' && !empty($args[1]))
                                {
                                    $controller = Controller_Categorie::getInstance();
                                    $controller->edit($args[1]);
                                }
                                else
                                {
                                    throw new Exception('Cattégorie non trouvée');
                                }
                            }
                            else
                            {
                                throw new Exception('Catégorie non trouvée');
                            }
                            break;
                        case 'new' :
                            $controller = Controller_Categorie::getInstance();
                            $controller->create();
                            break;
                        default:
                            throw new Exception('Page non trouvée');
                            break;
                        case 'delete' :
                            if(!empty($match[3])) //argument
                            {
                                $args = explode('&', $match[3]);
                                $args = explode('=', $args[0]);
                                $args[0] = trim($args[0]);
                                $args[1] = trim($args[1]);
                                if($args[0] == 'id' && !empty($args[1]))
                                {
                                    $controller = Controller_Categorie::getInstance();
                                    $controller->delete($args[1]);
                                }
                                else
                                {
                                    throw new Exception('Catégorie non trouvée');
                                }
                            }
                            else
                            {
                                throw new Exception('Catégorie non trouvée');
                            }
                            break;
                    }
                    break;
                case 'communes':
                    Config::set_slot('active', 'communes');

                    switch($action)
                    {
                        case 'index':
                            $controller = Controller_Commune::getInstance();
                            $controller->index();
                            break;
                        case 'edit':
                            if(!empty($match[3])) //argument
                            {
                                $args = explode('&', $match[3]);
                                $args = explode('=', $args[0]);
                                $args[0] = trim($args[0]);
                                $args[1] = trim($args[1]);
                                if($args[0] == 'id' && !empty($args[1]))
                                {
                                    $controller = Controller_Commune::getInstance();
                                    $controller->edit($args[1]);
                                }
                                else
                                {
                                    throw new Exception('Commune non trouvée');
                                }
                            }
                            else
                            {
                                throw new Exception('Commune non trouvée');
                            }
                            break;
                        case 'new' :
                            $controller = Controller_Commune::getInstance();
                            $controller->create();
                            break;
                        default:
                            throw new Exception('Page non trouvée');
                            break;
                        case 'delete' :
                            if(!empty($match[3])) //argument
                            {
                                $args = explode('&', $match[3]);
                                $args = explode('=', $args[0]);
                                $args[0] = trim($args[0]);
                                $args[1] = trim($args[1]);
                                if($args[0] == 'id' && !empty($args[1]))
                                {
                                    $controller = Controller_Commune::getInstance();
                                    $controller->delete($args[1]);
                                }
                                else
                                {
                                    throw new Exception('Commune non trouvée');
                                }
                            }
                            else
                            {
                                throw new Exception('Commune non trouvée');
                            }
                            break;
                    }
                    break;
             case 'actualites':
                    Config::set_slot('active', 'actualites');

                    switch($action)
                    {
                        case 'index':
                            $controller = Controller_Actualite::getInstance();
                            $controller->adminindex();
                            break;
                        case 'edit':
                            if(!empty($match[3])) //argument
                            {
                                $args = explode('&', $match[3]);
                                $args = explode('=', $args[0]);
                                $args[0] = trim($args[0]);
                                $args[1] = trim($args[1]);
                                if($args[0] == 'id' && !empty($args[1]))
                                {
                                    $controller = Controller_Actualite::getInstance();
                                    $controller->edit($args[1]);
                                }
                                else
                                {
                                    throw new Exception('Actualité non trouvée');
                                }
                            }
                            else
                            {
                                throw new Exception('Actualité non trouvée');
                            }
                            break;
                        case 'new' :
                            $controller = Controller_Actualite::getInstance();
                            $controller->create();
                            break;
                        default:
                            throw new Exception('Page non trouvée');
                            break;
                        case 'delete' :
                            if(!empty($match[3])) //argument
                            {
                                $args = explode('&', $match[3]);
                                $args = explode('=', $args[0]);
                                $args[0] = trim($args[0]);
                                $args[1] = trim($args[1]);
                                if($args[0] == 'id' && !empty($args[1]))
                                {
                                    $controller = Controller_Actualite::getInstance();
                                    $controller->delete($args[1]);
                                }
                                else
                                {
                                    throw new Exception('Actualité non trouvée');
                                }
                            }
                            else
                            {
                                throw new Exception('Actualité non trouvée');
                            }
                            break;
                    }
                    break;

                    case 'evenements':
                    Config::set_slot('active', 'evenements');

                    switch($action)
                    {
                        case 'index':
                            $controller = Controller_Agenda::getInstance();
                            $controller->adminindex();
                            break;
                        case 'edit':
                            if(!empty($match[3])) //argument
                            {
                                $args = explode('&', $match[3]);
                                $args = explode('=', $args[0]);
                                $args[0] = trim($args[0]);
                                $args[1] = trim($args[1]);
                                if($args[0] == 'id' && !empty($args[1]))
                                {
                                    $controller = Controller_Agenda::getInstance();
                                    $controller->edit($args[1]);
                                }
                                else
                                {
                                    throw new Exception('Evènement non trouvé');
                                }
                            }
                            else
                            {
                                throw new Exception('Evènement non trouvé');
                            }
                            break;
                        case 'new' :
                            $controller = Controller_Agenda::getInstance();
                            $controller->create();
                            break;
                        default:
                            throw new Exception('Page non trouvée');
                            break;
                        case 'delete' :
                            if(!empty($match[3])) //argument
                            {
                                $args = explode('&', $match[3]);
                                $args = explode('=', $args[0]);
                                $args[0] = trim($args[0]);
                                $args[1] = trim($args[1]);
                                if($args[0] == 'id' && !empty($args[1]))
                                {
                                    $controller = Controller_Agenda::getInstance();
                                    $controller->delete($args[1]);
                                }
                                else
                                {
                                    throw new Exception('Evènement non trouvé');
                                }
                            }
                            else
                            {
                                throw new Exception('Evènement non trouvé');
                            }
                            break;
                    }
                    break;

                case 'associations':
                    Config::set_slot('active', 'associations');

                    switch($action)
                    {
                        case 'index':
                            $controller = Controller_Association::getInstance();
                            $controller->index();
                            break;
                        case 'editinfo':
                            if(!empty($match[3])) //argument
                            {
                                $args = explode('&', $match[3]);
                                $args = explode('=', $args[0]);
                                $args[0] = trim($args[0]);
                                $args[1] = trim($args[1]);
                                if($args[0] == 'id' && !empty($args[1]))
                                {
                                    $controller = Controller_Association::getInstance();
                                    $controller->editinfo($args[1]);
                                }
                                else
                                {
                                    throw new Exception('Association non trouvée');
                                }
                            }
                            else
                            {
                                throw new Exception('Association non trouvée');
                            }
                            break;
                        case 'edit':
                            if(!empty($match[3])) //argument
                            {
                                $args = explode('&', $match[3]);
                                $args = explode('=', $args[0]);
                                $args[0] = trim($args[0]);
                                $args[1] = trim($args[1]);
                                if($args[0] == 'id' && !empty($args[1]))
                                {
                                    $controller = Controller_Association::getInstance();
                                    $controller->edit($args[1]);
                                }
                                else
                                {
                                    throw new Exception('Association non trouvée');
                                }
                            }
                            else
                            {
                                throw new Exception('Association non trouvée');
                            }
                            break;
                        case 'new' :
                            $controller = Controller_Association::getInstance();
                            $controller->create();
                            break;
                        default:
                            throw new Exception('Page non trouvée');
                            break;
                        case 'delete' :
                            if(!empty($match[3])) //argument
                            {
                                $args = explode('&', $match[3]);
                                $args = explode('=', $args[0]);
                                $args[0] = trim($args[0]);
                                $args[1] = trim($args[1]);
                                if($args[0] == 'id' && !empty($args[1]))
                                {
                                    $controller = Controller_Association::getInstance();
                                    $controller->delete($args[1]);
                                }
                                else
                                {
                                    throw new Exception('Association non trouvée');
                                }
                            }
                            else
                            {
                                throw new Exception('Association non trouvée');
                            }
                            break;
                    }
                    break;
                default:
                    throw new Exception('Page non trouvée');
                    break;
            }
        }
        catch(PDOException $ex)
        {
            $controller = Controller_Erreur::getInstance();
            $controller->show("Impossible d'effectuer la requête");
        }
        catch(Exception $ex)
        {
            $controller = Controller_Erreur::getInstance();
            $controller->show($ex->getMessage());
        }
    }
    else//pas de module donc page d'accueil
    {
            $controller = Controller_Admin::getInstance();
            $controller->index();
    }
}
else//pas de session ouverte donc login
{
    $controller = Controller_Login::getInstance();
    $controller->login();
}

//recupère le contenu dans une variable
$content = ob_get_contents();
ob_end_clean();

//affiche le layout qui affichera le contenu de la variable $content
//print $content;
require('view/admin-layout.php');
?>

